﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using ADSMS.DataAccess;
using ADSMS.BizLogic;
namespace ADSMS.Presentation.Store
{
    public partial class DisbursementList_Final : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((HttpContext.Current.User.IsInRole("Store Manager") || HttpContext.Current.User.IsInRole("Store Supervisor") || HttpContext.Current.User.IsInRole("Store Clerk")) != true)
            {

                Response.Redirect("/Presentation/Login.aspx");


            }

        }
        [WebMethod]
        public static List<DepartmentDisbDetails> GetDepartmentForDisb()
        {
            List<DepartmentDisbDetails> finaldisblist=new List<DepartmentDisbDetails>();
            DisbursementService_Web dser = new DisbursementService_Web();
            string status="Ready";
            DateTime checkdate=new DateTime();
            checkdate=DateTime.Today.AddDays(1);
           
            finaldisblist=dser.getDisbbyStatus(status, checkdate);

            return finaldisblist;
        }

        [WebMethod]
        public static List<object> getCollectionPointDetails(int DepID)
        {
            stationeryEntities se = new stationeryEntities();
            List<object> CPDetails = new List<object>();
            CPDetails = se.getCPointbyDepID(DepID).ToList<Object>();
            return CPDetails;




        }
        [WebMethod]
        public static string confirmDisb(List<int> quantdel, List<int> itemID, int DisbID)
        {
            stationeryEntities se = new stationeryEntities();
            int currentquant;
            int updatedquant;
            int newquant;
            item itemup = new item();
            string ans = "false";
            int length = quantdel.Count;
            for (int i = 0; i < length; i++)
            {
                disbursementdetail newdd = new disbursementdetail();
                int itemid = itemID[i];
                itemup = se.items.Find(itemid);
                var q = from dd in se.disbursementdetails
                        where dd.DisbID == DisbID &&
                        dd.DisbItemID == itemid
                        select dd;
                newdd = q.FirstOrDefault();

                currentquant = Convert.ToInt32(newdd.DisbItemQuantDelivered);
                updatedquant = quantdel[i];

                //updating invenotory
                if (updatedquant > currentquant)
                {
                    newquant = updatedquant - currentquant;
                    itemup.ItemQuant = itemup.ItemQuant - newquant;
                    se.SaveChanges();
                }
                else if (currentquant > updatedquant)
                {
                    newquant = currentquant - updatedquant;
                    itemup.ItemQuant = itemup.ItemQuant + newquant;
                    se.SaveChanges();

                }


                if (newdd.DisbItemQuantDelivered != quantdel[i])
                {
                    newdd.DisbItemQuantDelivered = quantdel[i];
                    se.SaveChanges();
                }




            }




            return ans;

        }

        //Send Email to multiple Departments
        protected void btn_SendEmail_Click(object sender, EventArgs e)
        {

            DisbursementService_Web dser = new DisbursementService_Web();
            dser.SendDisbEmail();
            
            
           


        }
    }
}